<template>
  <section class="main-section">
    <div class="btn-wrapper">
      <a-button class="btn-type" type="danger" @click="gen()">生成密钥</a-button>
      <a-button class="btn-type" type="primary" @click="sign()">签名</a-button>
      <a-button class="btn-type" type="primary" @click="verify()">验签</a-button>
    </div>

    <div>
      DSA公钥:
      <a-textarea placeholder="DSA公钥" :rows="4" :value="pubKeyText" allowClear/>
    </div>
    <a-divider type="vertical"/>

    <div>
      DSA私钥:
      <a-textarea placeholder="DSA私钥" :rows="4" :value="prvKeyText" allowClear/>
    </div>
    <a-divider type="vertical"/>

    <div>
      待签名数据:
      <a-input v-model:value="msg" placeholder="待签名数据"/>
    </div>
    <a-divider type="vertical"/>

    <div>
      验证数据:
      <a-input v-model:value="r" placeholder="签名后的r" style="margin-top: 20px"/>
      <a-input v-model:value="s" placeholder="签名后的s" style="margin-top: 20px"/>
      <a-input v-model:value="verifyMsg" placeholder="验证数据" style="margin-top: 20px"/>
    </div>
    <a-divider type="vertical"/>
  </section>
</template>

<script>
import {DSASign, DSAVerify, gen} from "../../api/dsa";

export default {
  name: "DSA",
  data() {
    return {
      r: '',
      s: '',
      prvKeyText: '',
      pubKeyText: '',
      prvKey: {},
      pubKey: {},
      msg: '',
      verifyMsg: ''
    }
  },
  methods: {
    gen() {
      gen().then(res => {
        let d = res
        console.log(`d: ${d}`)

        if (d == null) {
          this.$message.error(`DSA生成密钥失败`)
          return
        }

        // this.prvKey = JSON.stringify(d.prvKey)
        // this.pubKey = JSON.stringify(d.pubKey)
        this.prvKey = d.customPrvKey
        this.pubKey = d.customPubKey

        this.prvKeyText = JSON.stringify(this.prvKey)
        this.pubKeyText = JSON.stringify(this.pubKey)
        this.$message.success(`DSA生成密钥成功`)
      }).catch(err => {
        console.error(err)
        this.$message.error(`DSA生成密钥失败, 原因: ${err}`)
      })
    },
    // 签名
    sign() {
      let prvKey = this.prvKey
      let msg = this.msg

      if (prvKey.P === 0 || prvKey.Q === 0 || prvKey.G === 0 || prvKey.X === 0 || prvKey.Y === 0) {
        this.$message.error(`请检查私钥格式是否正确`)
        return
      }

      if (msg.length === 0) {
        this.$message.error(`签名数据不能为空`)
        return
      }

      console.log(prvKey)

      DSASign({
        "prvKey": prvKey,
        "msg": msg
      }).then(res => {
        if (res == null) {
          this.$message.error(`DSA签名失败`)
          return
        }

        this.r = res.r
        this.s = res.s
        this.$message.success(`DSA签名完成`)
      }).catch(err => {
        console.error(err)
        this.$message.error(`DSA生成密钥失败, 原因: ${err}`)
      })
    },
    verify() {
      let pubKey = this.pubKey
      let verifyMsg = this.verifyMsg
      let r = this.r
      let s = this.s

      if (pubKey.P === 0 || pubKey.Q === 0 || pubKey.G === 0 || pubKey.Y === 0) {
        this.$message.error(`请检查公钥格式是否正确`)
        return
      }

      if (verifyMsg.length === 0) {
        this.$message.error(`验签数据不能为空`)
        return
      }

      if (r.length === 0 || s.length === 9) {
        this.$message.error(`r或s不能为空`)
        return
      }

      DSAVerify({
        "pubKey": pubKey,
        "msg": verifyMsg,
        "r": r,
        "s": s
      }).then(res => {
        if (res == null) {
          this.$message.error(`DSA验签失败`)
          return
        }

        this.$message.success(`${res.message}, 验证结果: ${res.verify}`)
      }).catch(err => {
        console.error(err)
        this.$message.error(`DSA生成密钥失败, 原因: ${err}`)
      })
    }
  }
}
</script>

<style scoped>
.btn-wrapper {
  margin: 30px;
}

.btn-type {
  margin: 20px;
  line-height: 24px;
  font-size: 20px;
}

.main-section {
  margin-top: 30px;
}
</style>
